import xml.etree.ElementTree as ET
import requests

def pushbotwebhook(data):
    bothookweb = "xxx"
    r = requests.post(bothookweb, json=data)
    print(r.text)

def buildtotalmsg(buildid, **kw):
    strcontent = '''ExecutionTestReport：{}，请相关同事注意。
>Total :<font color="comment">{}例</font>
> Pass :<font color="info">{}例</font>
> Fail :<font color="warning">{}例</font>'''
    data = {
        "msgtype": "markdown",
        "markdown": {
            "content": strcontent.format(buildid, kw["total"], kw["pass"], kw["fail"])
        }
    }
    return data

def parsereport(reportfilename):
    tree = ET.parse(reportfilename)
    root = tree.getroot()
    if root.tag == "ResultsSession":
        totalEl = root.find("./ExecutedTestsDetails/Total")
        if totalEl:
            totalmsg = buildtotalmsg(root.attrib["buildId"], **totalEl.attrib)
            pushbotwebhook(totalmsg)
        else:
            print("can not find total")

if __name__ == "__main__":
    
    reportxml = "ExecutionTestReport.xml"
    parsereport(reportxml)